package tgc.edu.fhh.web.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.fhh.entity.SysUser;
import tgc.edu.fhh.security.UserUtils;
import tgc.edu.fhh.service.SysUserService;
import tgc.edu.fhh.web.form.Success;

@Controller
@RequestMapping(value = "/password")
public class PasswordController {

	@Autowired
	private SysUserService sysuserService;
	@Autowired
	private UserUtils userutils;
	
	@RequestMapping(value = "/edit")
	public String edit() {
		return "password/edit";
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public Success save(String pwd2) {
		SysUser user=userutils.getUser();
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
		String pwd=user.getPassword();
		if(encoder.matches(pwd2, pwd)) {
			return new Success(false, "新密码与旧密码相同！");
		}else {
			String password=encoder.encode(pwd2);
			user.setPassword(password);
			sysuserService.save(user);
			return new Success("修改成功！");
		}
		
	}
}
